#include "PosLoadUtil.h"
#include "entity/Position/BasePos.h"
#include "entity/Position/StarPos/StarPos.h"

PosLoadUtil* PosLoadUtil::m_posLoadUtil = NULL;

PosLoadUtil* PosLoadUtil::sharePosLoadUtil(){
	if(m_posLoadUtil == NULL){
		m_posLoadUtil = PosLoadUtil::create();
	}
	return m_posLoadUtil;
}

bool PosLoadUtil::init(){
	bool bRet = false;

	do{
		bRet = true;
	}while(0);

	return bRet;
}

CCArray* PosLoadUtil::loadPosWithFile(const char* s_filePath, EnumPosType enPosType, CCNode* container, int level, bool isDebug){

	CCArray* plistArray = CCArray::createWithContentsOfFile(s_filePath);

	CCObject* obj = NULL;
	CCDictionary* dict = NULL;

	CCString* x = NULL;
	CCString* y = NULL;

	CCArray* posList = CCArray::create();
	BasePos* basePos = NULL;

	CCARRAY_FOREACH(plistArray,obj){
		dict = dynamic_cast<CCDictionary*>(obj);

		x = dynamic_cast<CCString*>(dict->objectForKey("x"));
		y = dynamic_cast<CCString*>(dict->objectForKey("y"));

		if(x != NULL && y != NULL){
			switch (enPosType)
			{
			case enStarPos:
				basePos = StarPos::create(ccp(x->intValue(),y->intValue()),isDebug);
				break;
			}

			posList->addObject(basePos);

			if(container != NULL){
				container->addChild(basePos,level);
			}
		}
	}
	return posList;
}